package com.ifast.hnzk.controller;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ifast.common.domain.DictDO;
import com.ifast.common.service.DictService;
import com.ifast.common.utils.UUIDUtils;
import com.ifast.hnzk.domain.HnzkFile;
import com.ifast.hnzk.domain.PublishDO;
import com.ifast.hnzk.service.HnzkFileService;
import com.ifast.hnzk.service.PublishService;
import com.ifast.sys.domain.DeptDO;
import com.ifast.sys.domain.UserDO;
import com.ifast.sys.service.DeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.ifast.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2019-06-19 14:16:05 | wch</small>
 */
@Controller
@RequestMapping("/hnzk/publish")
public class PublishController extends BaseController {
	@Autowired
	private PublishService publishService;
	@Autowired
	private DeptService deptService;
	@Autowired
	private DictService dictService;
	@Autowired
	private HnzkFileService hnzkFileService;

	@GetMapping()
	@RequiresPermissions("hnzk:publish:publish")
	String Publish(Integer type ,Model model){
		if(type==1){
			model.addAttribute("userId",getUserId());
		}
	    return "hnzk/publish/publish";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("hnzk:publish:publish")
	public Result<Page<PublishDO>> list(PublishDO publishDTO){
		Wrapper<PublishDO> wrapper = new EntityWrapper<PublishDO>();
		//EntityWrapper wrapper = new EntityWrapper<>();
		//wrapper.setEntity(new PublishDO());
		wrapper.like("title",publishDTO.getTitle());
        wrapper.eq(publishDTO.getUserId()!=null,"userId",publishDTO.getUserId());
       /* List<Integer> statusList = new ArrayList<>();
        statusList.add(0);
        statusList.add(1);
        wrapper.in("status",statusList);*/
		wrapper.eq(publishDTO.getStatus()!=null,"status",publishDTO.getStatus());
		wrapper.orderBy("id", false);
        Page<PublishDO> page = publishService.selectPage(getPage(PublishDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("hnzk:publish:add")
	String add(Model model){
		UserDO userDO = getUser();
		model.addAttribute("userDo",userDO);
		/**
		 * 根据用户的部门ID查询得到部门名称
		 */
		Long deptId = userDO.getDeptId();
		DeptDO deptDO = deptService.selectById(deptId);
		model.addAttribute("deptDO",deptDO);
		/**
		 * 查询类型列表
		 */
		Wrapper<DictDO> wrapper = new EntityWrapper<DictDO>();
		wrapper.eq("type","hnzk_publish_type");
		List<DictDO> dictList = dictService.selectList(wrapper);
		model.addAttribute("dictList",dictList);
		List<DictDO> yesNoList = dictService.getYesNoList();
		model.addAttribute("yesNoList", yesNoList);

		/**生成一个UUID 作为附件ＩＤ以便和附件表进行关联*/
		String affixId = UUIDUtils.get();
		model.addAttribute("affixId", affixId);
	    return "hnzk/publish/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("hnzk:publish:edit")
	String edit(@PathVariable("id") Long id,Model model){
		PublishDO publish = publishService.selectById(id);
		model.addAttribute("publish", publish);
		List<DictDO> yesNoList = dictService.getYesNoList();
		model.addAttribute("yesNoList", yesNoList);
		Wrapper<DictDO> wrapper = new EntityWrapper<DictDO>();
		wrapper.eq("type","hnzk_publish_type");
		List<DictDO> dictList = dictService.selectList(wrapper);
		model.addAttribute("dictList",dictList);
		String affixId = publish.getAffixId();
		Wrapper<HnzkFile> wrapperFile = new EntityWrapper<HnzkFile>();
		wrapperFile.eq("fileFid",affixId);
		wrapperFile.eq("fileStatus",0);
		List<HnzkFile> hnzkFiles = hnzkFileService.selectList(wrapperFile);
		model.addAttribute("hnzkFiles", hnzkFiles);
	    return "hnzk/publish/edit";
	}

	@GetMapping("/view/{id}")
	String view(@PathVariable("id") Long id,Model model){
		PublishDO publish = publishService.selectById(id);
		model.addAttribute("publish", publish);
		DictDO dictDO = dictService.selectById(publish.getShowIndex());
		DictDO dictType = dictService.selectById(publish.getType());
		model.addAttribute("dictDO", dictDO);
		String affixId = publish.getAffixId();
		Wrapper<HnzkFile> wrapper = new EntityWrapper<HnzkFile>();
		wrapper.eq("fileFid",affixId);
		wrapper.eq("fileStatus",0);
		List<HnzkFile> hnzkFiles = hnzkFileService.selectList(wrapper);
		model.addAttribute("hnzkFiles", hnzkFiles);
		return "hnzk/publish/view";
	}
	
	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("hnzk:publish:add")
	public Result<String> save( PublishDO publish){
		publish.setStatus(0);
		publish.setCreateDate(new Date());
		Long type = publish.getType();
		publish.setApprovalType(0);
		DictDO dictDO = dictService.selectById(type);
		publish.setTypeName(dictDO.getName());

		publishService.insert(publish);
        return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("hnzk:publish:edit")
	public Result<String>  update( PublishDO publish){
		Long type = publish.getType();
		publish.setApprovalType(0);
		DictDO dictDO = dictService.selectById(type);
		publish.setTypeName(dictDO.getName());
		if(publish.getApprovalMessage()!=null && !publish.getApprovalMessage().isEmpty()){
			publish.setApprovalType(1);
		}
		boolean update = publishService.updateById(publish);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("hnzk:publish:remove")
	public Result<String>  remove( Long id){
		publishService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("hnzk:publish:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		publishService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
